<template>
  <div>
    <!-- 总部资源管理 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTables
      ref="table"
      columnsRef="zjDetailsInquiry"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :pageSizesArray="[20, 40, 100, 200, 500]"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls">
        <!-- <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 1)" :disabled="qualified != 2">合格</multiuse-button>
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 2)" :disabled="qualified != 1">不合格</multiuse-button> -->
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 3)" :disabled="lockDisable != 2">锁 定</multiuse-button>
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 4)" :disabled="lockDisable != 1">解 锁</multiuse-button>
        <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
      </template>
    </FilterTables>
    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher @close="ownercloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isSelectOrder" :title="'配车'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <selectOrder :rowData="formData" @close="selectHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="isLock" :title="'锁定'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <lockModel :list="selectList" @close="closeHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, vehicleConfig, getFactoryWarehouseList, getWarehouseList, exportExcel, confirmQualified, confirmLock } from '@/api/omSale/HeadquartersInventory/HeadquartersResources';
import { exportFile } from '@/utils/index';
import ownerSeacher from './ownerSeacher';
import selectOrder from './selectOrder';
import lockModel from './lockModel';
export default {
  name: 'DetailsInquiry',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {
        xhrAbort: true
      },
      formData: {},
      issearch: false,
      dealerCode: '',
      exportLoading: false,
      isSelectOrder: false,
      selectList: [],
      lockDisable: 0,
      qualified: 0,
      isLock: false
    };
  },
  components: {
    ownerSeacher,
    selectOrder,
    lockModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.getFactoryList();
    this.getList();
    this.vehicleInfo();
  },

  methods: {
    // 工厂库
    async getFactoryList() {
      const res = await getFactoryWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map(x => {
          this.topFilterList.find(item => item.fieldName === 'warehouseId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },
    // 仓库名称
    async getList() {
      const res = await getWarehouseList();
      if (res.resultCode == 200) {
        res.data.list.map(x => {
          this.topFilterList.find(item => item.fieldName === 'subWarehouseId').itemList.push({ text: x.warehouseName, value: x.warehouseId });
        });
      }
    },
    // 车型选择
    async vehicleInfo() {
      let res = await vehicleConfig(0, { isUnlimited: 10041001 });
      this.topFilterList.find(item => item.fieldName === 'vehicleInfo').itemList = res.data;
    },
    // 导出
    async exportHandle() {
      await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLoading = true;

          exportExcel(this.fetchParams)
            .then(response => {
              exportFile(response.data, '总部库存明细查询', 'csv');
              this.exportLoading = false;
            })
            .catch(err => {
              console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    seacherFun() {
      this.issearch = true;
    },
    ownercloseHandler(val) {
      if (val) {
        console.log(val);
        this.topFilterList.map(x => {
          if (x.fieldName == 'dealerName') {
            x.initialValue = val.dealerName;
          }
        });
        this.dealerCode = val.dealerCode;
      }
      this.issearch = false;
    },
    changeHandle(params) {
      this.qualified = 0;
      this.lockDisable = 0;
      this.fetchParams.dealerCode = this.dealerCode;
      this.fetchParams.xhrAbort = false;

      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    resetChange(val) {
      this.dealerCode = '';
      this.fetchParams.dealerCode = '';
      this.qualified = 0;
      this.lockDisable = 0;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    editHandle(data) {
      this.formData = data;
      this.isSelectOrder = true;
    },
    selectHandler(val) {
      if (val) {
        this.fetchParams.dealerCode = this.dealerCode;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
      this.isSelectOrder = false;
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        let isOpen = 0;
        let isClose = 0;
        let isOpens = 0;
        let isCloses = 0;
        row.map(x => {
          if (x.isLock == 10041001) {
            isOpen = isOpen + 1;
          } else {
            isClose = isClose + 1;
          }
          if (x.isQualified == '合格') {
            isOpens = isOpens + 1;
          } else {
            isCloses = isCloses + 1;
          }
        });
        if (isOpen > 0 && isClose == 0) {
          this.lockDisable = 1;
        } else if (isOpen == 0 && isClose > 0) {
          this.lockDisable = 2;
        } else {
          this.lockDisable = 0;
        }
        if (isOpens > 0 && isCloses == 0) {
          this.qualified = 1;
        } else if (isOpens == 0 && isCloses > 0) {
          this.qualified = 2;
        } else {
          this.qualified = 0;
        }
        this.selectList = row;
      } else {
        this.qualified = 0;
        this.lockDisable = 0;
        this.selectList = [];
      }
    },
    async addHandle(type) {
      if (type == 1) {
        await this.$confirm('是否合格?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectList.map(x => {
              arr.push(`${x.id}`);
            });
            let res = await confirmQualified({
              ids: arr,
              isQualified: '合格'
            });

            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '合格成功',
                type: 'success'
              });
              this.qualified = 0;
              this.lockDisable = 0;
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } else if (type == 2) {
        await this.$confirm('是否不合格?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectList.map(x => {
              arr.push(`${x.id}`);
            });
            let res = await confirmQualified({
              ids: arr,
              isQualified: '不合格'
            });

            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '不合格成功',
                type: 'success'
              });
              this.qualified = 0;
              this.lockDisable = 0;
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } else if (type == 3) {
        this.isLock = true;
      } else if (type == 4) {
        await this.$confirm('是否解锁?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectList.map(x => {
              arr.push(`${x.id}`);
            });
            let res = await confirmLock({
              ids: arr,
              lockStatus: 10041002
            });

            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '解锁成功',
                type: 'success'
              });
              this.qualified = 0;
              this.lockDisable = 0;
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      }
    },
    closeHandler(val) {
      this.isLock = false;
      if (val) {
        this.qualified = 0;
        this.lockDisable = 0;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请选择',
          readonly: true,
          hidden: true,
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.dealerCode = '';
            }
          }
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        // {
        //   type: 'SELECT',
        //   label: '工厂库',
        //   fieldName: 'warehouseId',
        //   placeholder: '请选择',
        //   itemList: []
        // },
          {
          type: 'INPUT',
          label: '批售订单号',
          fieldName: 'orderNo',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '车辆状态',
          fieldName: 'vehicleStatus',
          placeholder: '请选择',
          itemList: this.createDictList(2413)
        },
         {
          type: 'INPUT',
          label: '物料编码',
          fieldName: 'productCode',
          placeholder: '请输入',
        },
          {
          type: 'INPUT',
          label: '物料描述',
          fieldName: 'productName',
          placeholder: '请输入',
        },
         {
            type: 'SELECT',
            label: '车系名称',
            fieldName: 'seriesId',
            placeholder: '请选择',
            itemList: []
          },

         {
            type: 'SELECT',
            label: '车型名称',
            fieldName: 'modelName',
            placeholder: '请选择',
            itemList: []
          },
          {
          type: 'SELECT',
          label: '配置名称',
          fieldName: 'packageId',
          placeholder: '请选择',
          itemList:[]
        },
          {
          type: 'SELECT',
          label: '选装名称',
          fieldName: 'xzmc',
          placeholder: '请选择',
          itemList:[]
        },
        {
          type: 'SELECT',
          label: '内饰',
          fieldName: 'trimCode',
          placeholder: '请选择',
          itemList:[]
        },
        {
          type: 'SELECT',
          label: '外饰',
          fieldName: 'colorCode',
          placeholder: '请选择',
          itemList:[]
        },
         {
          type: 'SELECT',
          label: '是否锁定',
          fieldName: 'isLock',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
         {
          type: 'SELECT',
          label: '订单占用',
          fieldName: 'matchFlag',
          placeholder: '请选择',
          itemList: [
            { text: '占用', value: 10041001 },
            { text: '可用', value: 10041002 }
          ]
        },
         {
          type: 'SELECT',
          label: '批售退车标记',
          fieldName: 'returnFlag',
          placeholder: '请选择',
          itemList: this.createDictList('1004')
        },
         {
          type: 'SELECT',
          label: '公告号',
          fieldName: 'announceNo',
          placeholder: '请选择',
          itemList:[]
         
        },
      
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        // {
        //   title: '操作',
        //   dataIndex: 'column-action',
        //   width: 50,
        //   fixed: 'left',
        //   render: (props, h) => {
        //     return (
        //       <div>
        //         <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)} disabled={props.row.matchFlag == 10041001 || props.row.isLock == 10041001}>
        //           配车
        //         </multiuse-button>
        //       </div>
        //     );
        //   }
        // },
         {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '是否锁定',
          dataIndex: 'isLock',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isLock, '1004')}</span>;
          }
        },
         {
          title: '锁定备注',
          dataIndex: 'lockRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '锁定人',
          dataIndex: 'lockPerson',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '锁定时间',
          dataIndex: 'lockDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '车系名称',
          dataIndex: 'seriesName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '选装名称',
          dataIndex: 'optionName',
          align: 'center',
          width: 250,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
          {
          title: '内饰',
          dataIndex: 'trimCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰',
          dataIndex: 'colorCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
          {
          title: '物料编码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '物料描述',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '公告号',
          dataIndex: 'announceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '市场指导价',
          dataIndex: 'basicPrice',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '是否三方车辆',
          dataIndex: 'isTripartite',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.isTripartite, '1004')}</span>;
          }
        },
         {
          title: '三方票号',
          dataIndex: 'biilNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '车辆状态',
          dataIndex: 'vehicleStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
       //   filterItems: [{ text: '车厂库存', value: 60401001 }],
          filterItems:this.createDictList('2413'),
          render: props => {
            return <span>{this.createDictText(props.row.vehicleStatus, '2413')}</span>;
          }
        },
         {
          title: '订单占用',
          dataIndex: 'matchFlag',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '占用', value: 10041001 },
            { text: '可用', value: 10041002 }
          ],
          render: props => {
            return <span>{props.row.matchFlag == 10041001 ? '占用' : '可用'}</span>;
          }
        },
        {
          title: '批售订单号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
          {
          title: '已审订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        // {
        //   title: '工厂库',
        //   dataIndex: '',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input'
        // },
          {
          title: '库位',
          dataIndex: 'STORAGE_POSITION_CODE',
          showOverflowTooltip: true,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '生产下线日期',
          dataIndex: 'productDate',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
        {
          title: '入库日期',
          dataIndex: 'vehicleCreatedAt',
          sorter: true,
          filter: true,
          filterType: 'date-range',
          dateFormat: 'yyyy-MM-dd'
        },
         {
          title: '车龄(天)',
          dataIndex: 'productDateAge',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '库龄(天)',
          dataIndex: 'postingDateAge',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
         {
          title: '发动机号',
          dataIndex: 'engineNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电机号',
          dataIndex: 'motorNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '电池号',
          dataIndex: 'batteryNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '合格证号',
          dataIndex: 'certificateNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
          {
          title: '退车标识',
          dataIndex: 'returnFlag',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: props => {
            return <span>{this.createDictText(props.row.returnFlag, '1004')}</span>;
          }
        },
      ];
    }
  }
};
</script>
